package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFiegnClient;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-05-18
 */
 //允许跨域访问
@RestController // Controller+ResponseBody
@Api(tags = "讲师管理模块") //描述controller
//http://localhost:8110/admin/edu/teacher/batchRemove?ids=1,10'
// http://localhost:8110/admin/edu/teacher/batchRemove
@RequestMapping("/admin/edu/teacher")
public class AdminTeacherController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    OssFiegnClient ossFiegnClient;
    //使用feignclient远程访问测试接口
    @GetMapping("test")
    public R test(){
//        R ossR = ossFiegnClient.test("anni" , "123456");
        R ossR = ossFiegnClient.test(R.setResultCodeEnum(ResultCodeEnum.PARAM_ERROR));
        System.out.println("ossR = " + ossR);
        return R.ok();
    }



    //6、批量删除讲师
    @ApiOperation("批量删除讲师")
    @DeleteMapping("batchRemove")  //  url?hobby=basketball&hobby=pp&hobby=football
    // springmvc:  url?ids=1,2,3,4,5     List<Integer> ids
    public R batchRemove(@RequestParam List<String> ids){ //集合类型接受请求参数 必须使用@RequestParam注解绑定
        teacherService.removeByIds(ids);
        return R.ok();
    }


    //3、新增讲师
    @PostMapping("save") // @RequestBody表示从请求体中获取json格式的参数转为指定类型的对象
    public R saveTeacher(@RequestBody Teacher teacher){
        teacherService.save(teacher);
        return R.ok();
    }
    //4、更新讲师
    @PutMapping("update")
    public R updateTeacher(@RequestBody Teacher teacher){
        teacherService.updateById(teacher);
        return R.ok();
    }
    //5、分页查询
    //get方式 只能通过路径参数或者 请求query参数方式提交  url?k=v
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@PathVariable Integer pageNum ,
                      @PathVariable Integer pageSize,
                      TeacherQuery teacherQuery //如果访问时url?k=v 提交参数，参数属性名和javabean的属性名一样，可以直接pojo入参
        ){
        Page<Teacher> page = new Page<>(pageNum , pageSize);
        page = teacherService.getTeacherPageByCondition(page , teacherQuery);
        return R.ok().setData("page" , page);
    }

    /*
        讲师管理接口
     */
    //2、根据id查询讲师
    // get 请求没有请求体
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("getById/{id}") // getById?id=xxx    请求参数query
    // getById/1   path  路径参数
    // post请求方式还支持请求体传参： body   ： k=v ， json
    public R getById(@ApiParam(required = true , value = "用户id" ,defaultValue = "1") @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().setData("item" , teacher);
    }
    //3、删除讲师
    @ApiOperation("根据id删除讲师")
    @DeleteMapping("removeById/{id}")
    public R removeById(@PathVariable String id){
        // 在删除之前 根据id查询讲师数据
        Teacher teacher = teacherService.getById(id);
        boolean b = teacherService.removeById(id);
        if(b){
            //删除讲师头像： 传入讲师数据 头像地址+模块
            if(!StringUtils.isEmpty(teacher.getAvatar())){
                ossFiegnClient.delete(teacher.getAvatar(),"avatar");
                //设置讲师avatar的值为空，需要自定义sql[学了自定义sql以后 你们自己实现]
            }
            return R.ok();
        }
        return R.error().setMessage("记录不存在");
    }
    @Value("${server.port}")
    private Integer port;
    //1、查询所有的讲师
    @ApiOperation("查询所有讲师")
    @GetMapping("list")
    public R listAll(){
        System.out.println("port = " + port);
        List<Teacher> list = teacherService.list();
        return R.ok().setData("items" , list);
    }
}

